<!DOCTYPE html>
<html lang='en'>

<head>
    <meta charset='UTF-8'>
    <meta http-equiv='X-UA-Compatible' content='IE=edge'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>Document</title>
    <script src='https://cdn.staticfile.org/react/16.4.0/umd/react.development.js'></script>
    <script src='https://cdn.staticfile.org/react-dom/16.4.0/umd/react-dom.development.js'></script>
    <!-- 生产环境中不建议使用 -->
    <script src='https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js'></script>
    <!-- PropTypes文件 -->
    <script src='https://unpkg.com/prop-types@15.6.2/prop-types.js'></script>
</head>

<body>
    <div id='app'>
    </div>
    <script type='text/babel'>
        // 生命周期钩子函数 = 生命周期钩子 = 生命周期函数
        class Demo extends React.Component{
            state = {opacity:1}

            destory = ()=>{
                // react中通过ReactDOM.unmountComponentAtNode去卸载组件，参数为这个组件挂载的容器。
                ReactDOM.unmountComponentAtNode(document.getElementById('app'))
            }

            // componentDidMount这个生命周期在组件挂载到容器上后执行
            componentDidMount(){
                this.timer = setInterval(() => {
                    console.log(1)
                    let {opacity} = this.state
                    if(opacity>0){
                        opacity = opacity-0.1
                        this.setState({opacity})
                    }
                    else{
                        opacity = 1
                    }
                }, 200);
            }

            // componentWillUnmount这个生命周期在组件将要被摧毁或者说嗝屁之前执行
            componentWillUnmount(){
                clearInterval(this.timer)
            }

            render(){
                return (
                    <div>
                        <h2 style={{opacity:this.state.opacity}}>学不会怎么办</h2>
                        <button onClick={this.destory}>毁灭吧</button>    
                    </div>
                )
            }
        }

        ReactDOM.render(<Demo/>,document.getElementById('app'))
    </script>
</body>

</html>